package cn.merryyou.generator.service.utils;

import cn.merryyou.generator.config.GenConfig;
import cn.merryyou.generator.domain.TableInfo;
import cn.merryyou.generator.service.GenInfo;
import cn.merryyou.generator.service.GenUtils;
import cn.merryyou.generator.util.DateUtils;
import org.apache.velocity.VelocityContext;

import java.util.ArrayList;
import java.util.List;

public class GenInterFfcs implements GenInterfaces {


    @Override
    public VelocityContext getVelocityContext(GenInfo genInfo, TableInfo table) {
        // java对象数据传递到模板文件vm
        VelocityContext velocityContext = new VelocityContext();
        String packageName = GenConfig.getPackageName();
        velocityContext.put("tableName", table.getTableName());
        velocityContext.put("tableComment", GenUtils.replaceKeyword(table.getTableComment()));
        velocityContext.put("primaryKey", table.getPrimaryKey());
        velocityContext.put("className", table.getClassName());
        velocityContext.put("classname", table.getClassname());
        velocityContext.put("classname_lower", table.getClassname().toLowerCase());
        velocityContext.put("moduleName", GenUtils.getModuleName(packageName));
        velocityContext.put("columns", table.getColumns());
        velocityContext.put("basePackage", GenUtils.getBasePackage(packageName));
        velocityContext.put("package", packageName);
        velocityContext.put("author", GenConfig.getAuthor());
        velocityContext.put("datetime", DateUtils.getDate());
        return velocityContext;
    }

    @Override
    public List<String> getTemplates(String path) {
        List<String> templates = new ArrayList<String>();
        templates.add("vm/ffcs/html/list.html.vm");
        templates.add("vm/ffcs/html/input.html.vm");
        templates.add("vm/ffcs/java/Controller.java.vm");
        templates.add("vm/ffcs/java/Dao.java.vm");
        templates.add("vm/ffcs/java/DaoImpl.java.vm");
        templates.add("vm/ffcs/java/Domain.java.vm");
        templates.add("vm/ffcs/java/DomainEvt.java.vm");
        templates.add("vm/ffcs/java/Service.java.vm");
        templates.add("vm/ffcs/java/ServiceImpl.java.vm");
        templates.add("vm/ffcs/java/sql.vm");
        return templates;
    }

    @Override
    public String getFileName(GenInfo genInfo, String template, TableInfo table) {
        // 小写类名
        String classname = table.getClassname();
        // 大写类名
        String className = table.getClassName();

        if (template.contains("list.html.vm")) {
            return "jsp/" + classname.toLowerCase() + "/list.jsp";
        }
        if (template.contains("input.html.vm")) {
            return "jsp/" + classname.toLowerCase() + "/input.jsp";
        }
        if (template.contains("Controller.java.vm")) {
            return "controller/" + classname.toLowerCase() + "/" + className + "Controller.java";
        }
        if (template.contains("Service.java.vm")) {
            return "service/" + classname.toLowerCase() + "/" + className + "Service.java";
        }
        if (template.contains("ServiceImpl.java.vm")) {
            return "service/" + classname.toLowerCase() + "/" + className + "ServiceImpl.java";
        }
        if (template.contains("Dao.java.vm")) {
            return "service/" + classname.toLowerCase() + "/" + className + "Dao.java";
        }
        if (template.contains("DaoImpl.java.vm")) {
            return "service/" + classname.toLowerCase() + "/" + className + "DaoImpl.java";
        }
        if (template.contains("Domain.java.vm")) {
            return "/entity/" + className + ".java";
        }
        if (template.contains("DomainEvt.java.vm")) {
            return "service/" + classname.toLowerCase() + "/evt/" + className + "Evt.java";
        }
        if (template.contains("sql.vm")) {
            return "sql/menu.sql";
        }
        return null;
    }


}
